package org.dei.perla.rockfall.gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import org.dei.perla.rockfall.RockfallManager;
import org.dei.perla.utils.PerlaRuntime;

/**
 * JInternalFrame per eseguire il submit di una query
 */
public class QueryWindow extends JInternalFrame implements ActionListener{

	private static final long serialVersionUID = 8488836016707332137L;
	
	private static final String DEFAULT_TEXT = "SET acquisitionType=0, command=0, rs1=0x2101, rs2=0x1787, rs3=5, rs4=0x80, \ngx=68 , gy=69, gz=30, thupX=0xf, thupY=0xf, \nthupZ=0xf, thdwX=0x3f, thdwY=0x3f, thdwZ=0x3f, \ntaps = NEW(CONSTANTVECTORINTEGER, \"3, 10, 5\") \nON 1";
	
	private final JTextPane textPane;
	private final RockfallManager manager;
	private FileInputStream fin;
	private FileOutputStream fout;
	/**
	 * Costruttore
	 * @param manager RockfallManager per la gestione del sistema
	 */
	public QueryWindow(RockfallManager manager) {
		super("Query Window");
		setMaximizable(true);
		setIconifiable(true);
		setResizable(true);
		setClosable(false);
		
		this.manager = manager;
		
		getContentPane().setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		textPane = new JTextPane();
		PerlaRuntime perlaRuntime = PerlaRuntime.getDefaultRuntime();
		String defaultQuery = null;
		
		if((defaultQuery = perlaRuntime.getProperty(PerlaRuntime.PERLA_LAST_QUERY_EXECUTED)) == null)
			textPane.setText(DEFAULT_TEXT);
		else
			textPane.setText(defaultQuery);
			
		JScrollPane scrollPane = new JScrollPane(textPane);
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.gridwidth = 3;
		gbc.weightx = 0.5;
		gbc.weighty = 0.5;
		gbc.fill = GridBagConstraints.BOTH;
		getContentPane().add(scrollPane, gbc);
		
		JButton button = new JButton("Cancel");
		button.setSize(new Dimension(100, 50));
		button.addActionListener(this);
		button.setActionCommand("cancel");
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.weightx = 0;
		gbc.weighty = 0;
		gbc.gridwidth = 1;
		gbc.fill = GridBagConstraints.NONE;
		gbc.anchor = GridBagConstraints.LAST_LINE_END;
		getContentPane().add(button, gbc);
		
		button = new JButton("Execute");
		button.setSize(new Dimension(100, 50));
		button.addActionListener(this);
		button.setActionCommand("execute");
		gbc.gridx = 2;
		gbc.anchor = GridBagConstraints.FIRST_LINE_START;
		getContentPane().add(button, gbc);
		
		button = new JButton("Save query");
		button.setSize(new Dimension(100, 50));
		button.addActionListener(this);
		button.setActionCommand("saveQuery");
		gbc.gridx = 3;
		gbc.anchor = GridBagConstraints.FIRST_LINE_START;
		getContentPane().add(button, gbc);
		
		
		pack();
		setSize(new Dimension(800, 600));
	}

	/**
	 * Metodo di implementazione ActionListener
	 */
	public void actionPerformed(ActionEvent event) {
		
		if (event.getActionCommand().equals("cancel")) {
			
			textPane.setText(DEFAULT_TEXT);
			
		} else if (event.getActionCommand().equals("execute")) {
			
			manager.executeQuery(textPane.getText());
		} else if (event.getActionCommand().equals("saveQuery")){
			PerlaRuntime perlaRuntime = PerlaRuntime.getDefaultRuntime();
			perlaRuntime.setProperty(PerlaRuntime.PERLA_LAST_QUERY_EXECUTED, textPane.getText());
		}
	}
}
